﻿using jinmaMesProxy.DataAccess;
using jinmaMesProxy.Excep;
using jinmaMesProxy.Model;
using System.Reflection;
using System.Text;

namespace jinmaMesProxy.Service
{
    public class MaterialAppVouchService
    {
        ErpDbInstance dbInstance;
        ErpVouchQuery<MaterialAppVouch,MaterialAppVouchs> query;
        public MaterialAppVouchService()
        {
            dbInstance = ServiceLoader.GetService<ErpDbInstance>();
            ps = typeof(MaterialAppVouch).GetProperties();
            query = new ErpVouchQuery<MaterialAppVouch, MaterialAppVouchs>();
        }
        PropertyInfo[] ps;

        public  bool Exists(string billCode)
        {
            return query.Exists(billCode);
        }
        
        public MaterialAppVouch GetByCode(string code)
        {
            var result = query.GetByCode(code);
            if (result == null) throw new MyException($"找不到名为{code}的需求单");
            return result;
        }

        public void Delete(string code)
        {
            string delSql1 = "delete from MaterialAppVouchs where id = (select id from MaterialAppVouch where ccode = @p1 and chandler is null)";
            string delSql2 = "delete from MaterialAppVouch where ccode = @p1 and chandler is null";
            using (var cmd1 = dbInstance.MakeQueryCommand(delSql1, new KeyValuePair<string, object?>("@p1", code)))
            {
                var res = cmd1.ExecuteNonQuery();
                if (res == 0) throw new MyException("单据状态已审核或者没有这个单据");
            }
            using (var cmd2 = dbInstance.MakeQueryCommand(delSql2, new KeyValuePair<string, object?>("@p1", code)))
            {
                var res = cmd2.ExecuteNonQuery();
                if (res == 0) throw new MyException("单据状态已审核或者没有这个单据");
            }
            Logger.Log($"单据{code}已被删除");
        }
        public void Save(MaterialAppVouch data)
        {
            MaterialService materialService = new MaterialService();

            var updater = new ErpDbUpdater<MaterialAppVouch, MaterialAppVouchs>();
            var (id, did) = SimpleSqlHelper.GetId("mv");
            id++;
            data.Id = 1000000000 + id;
            int i = 1;
            if (String.IsNullOrWhiteSpace(data.BillCode)) throw new MyException("必须指定单号");
            if (this.Exists(data.BillCode)) throw new MyException("单号已经存在");
            if (data.Details.Count == 0) throw new MyException("不允许没有明细行的单据");
            foreach (var item in data.Details)
            {
                did++;
                item.AutoId = 1000000000 + did;
                item.RowNo = i++;
                if (item.Quantity <= 0) throw new MyException($"物料{item.Material}数量有误");
                var material = materialService.GetByCode(item.Material);
                if (String.IsNullOrWhiteSpace(item.Warehouse))
                {
                    item.Warehouse = material.Warehouse;
                    if (String.IsNullOrWhiteSpace(item.Warehouse)) throw new MyException($"物料{item.Material}没有设置仓库");
                }
                else
                {
                    SimpleSqlHelper.CheckWarehouse(item.Warehouse);//利用代码检查是否存在合法仓库，不存在抛异常，不需要赋值
                }
                
                materialService.CheckFree(material, item);
                materialService.CheckFreeValue(item);//不成功的话里面会报异常

            }
            data.TempId = SimpleSqlHelper.GetTempId("0413");
            updater.Insert(data);
            SimpleSqlHelper.WriteId("mv", id, did);
            Logger.Log($"单据{data.BillCode}已被创建");
        }
    }

}
